//
//  ViewController.m
//  英雄展示
//
//  Created by song on 15/1/10.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "JOHero.h"

@interface ViewController () <UITableViewDataSource ,UITableViewDelegate ,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) NSArray *heros;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    self.tableView.delegate =self;
    self.tableView.separatorColor =[UIColor blueColor];
    
    self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    // 设置没一行的高度
    //self.tableView.rowHeight = 60;
}

-(NSArray *)heros
{
    if(_heros == nil)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        NSArray *dictarray =[NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *marray =  [NSMutableArray array];
        
        for(NSDictionary *dic  in dictarray)
        {
            JOHero *hero = [JOHero heroWithDic:dic];
            [marray addObject:hero];
        }
        _heros = marray;
    }
    return _heros;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 97;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JOHero *hero = self.heros[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"A"];
    
    if(cell ==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"A"];
//        NSLog(@"-----缓存找不到cell cell ---%d", indexPath.row);
    }

    
    cell.textLabel.text =hero.name;
    cell.detailTextLabel.text =hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    
    cell.accessoryType =UITableViewCellAccessoryCheckmark;
    
    cell.accessoryView = [[UISwitch alloc]init];
    
    
    UIView *bgview = [[UIView alloc]init];
    bgview.backgroundColor = [UIColor redColor];
    cell.backgroundView  =bgview;
    
    UIView *selectview = [[UIView alloc]init];
    selectview.backgroundColor = [UIColor greenColor];
    cell.selectedBackgroundView = selectview;
    
//    NSLog(@" %p " ,cell);
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"-----选择的  cell ---%d", indexPath.row);
    JOHero *hero = self.heros[indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"数据展示" message:hero.name delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    alert.alertViewStyle =UIAlertViewStylePlainTextInput;
    [alert textFieldAtIndex:0].text = hero.name;
    [alert show];
    alert.tag = indexPath.row;
}





#pragma mark
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)  return ;
    
    NSString *name = [alertView textFieldAtIndex:0].text;
    
    int row = alertView.tag;
    JOHero *hero = self.heros[row];
    hero.name = name;
    
//    [self.tableView reloadData];
    
    
    // 数据的局部刷新
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

//-(void)tableview


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row ==0 ) return 100;
    
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
